import { Toast } from '@douyinfe/semi-ui'

export function resultHandler(response: any) {

    if ((response.status !== 200 && response.status !== 201)
      || response.config.method === "get"
      || (response.config.method === "post"
        && response.config.data
        && response.config.data.toString().indexOf("pageNum") !== -1)
    ) {
      return
    }
  
    // // 如果是登录的接口
    if (response.config.method === "post"
      && (response.config.url.indexOf("/user/loginPC") > -1)) {
      if (response.status === 200) {
        Toast.success(`欢迎使用！`)
      }
      return
    }
  
    let code = Number(response.status)

    console.log("error",code)

    // @ts-ignore
    switch (code) {
      case 200: {
        Toast.success("操作成功")
        return
      }
      case 201: {
        Toast.success("操作成功")
        return
      }
      default: {
        // 修复data内也有status引起的操作失败问题
        if (response.status !== 200 && response.status !== 201) {
          
            const { message, title } = response.data
            const errorReason = message || title ? `"${message || title}"` : false
            Toast.error(`操作失败 - ${(errorReason && `, ${errorReason}`)}`)
          
        }
  
      }
    }
  }